﻿using System;

namespace SCG.General
{
    public class SCGColor
    {
        public SCGColor()
        {
        }
        public SCGColor(byte a, byte r, byte g, byte b)
        {
            A = a;
            R = r;
            G = g;
            B = b;
        }
        public SCGColor(byte r, byte g, byte b)
        {
            A = 255;
            R = r;
            G = g;
            B = b;
        }
        public SCGColor(string s)
        {
            string[] tokens = s.Split(',');
            A = 255;
            R = Byte.Parse(tokens[0]);
            G = Byte.Parse(tokens[1]);
            B = Byte.Parse(tokens[2]);
        }

        public byte A { get; set; }
        public byte R { get; set; }
        public byte G { get; set; }
        public byte B { get; set; }

        public string CompareCode
        {
            get
            {
                return (R / 40) + "" + (G / 40) + "" + (B / 40);
            }
        }

        public bool SimilarTo(SCGColor c)
        {
            return CompareCode == c.CompareCode;
        }

        public int Luminance
        {
            get
            {
                return (int)((0.2126 * R) + (0.7152 * G) + (0.0722 * B));
            }
        }

        public void Brighten()
        {
            if (R < 245)
                R += 10;
            if (G < 245)
                G += 10;
            if (B < 245)
                B += 10;
        }

        public override string ToString()
        {
            return R + "," + G + "," + B;
        }

        public override bool Equals(object obj)
        {
            if (obj is SCGColor)
            {
                SCGColor c = (SCGColor)obj;
                return c.A == A && c.R == R && c.G == G && c.B == B;
            }
            else
                return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return A * 255 + R * 128 + G * 64 + B;
        }

        public static bool operator ==(SCGColor a, SCGColor b)
        {
            return a.Equals(b);
        }
        public static bool operator !=(SCGColor a, SCGColor b)
        {
            return !(a.Equals(b));
        }

        public static SCGColor Transparent = new SCGColor(0, 0, 0, 0);
        public static SCGColor Black = new SCGColor(255, 0, 0, 0);
        public static SCGColor White = new SCGColor(255, 255, 255, 255);
        public static SCGColor Gray = new SCGColor(255, 128, 128, 128);
        public static SCGColor Silver = new SCGColor(255, 192, 192, 192);
        public static SCGColor Red = new SCGColor(255, 255, 0, 0);
        public static SCGColor Green = new SCGColor(255, 0, 128, 0);
        public static SCGColor Blue = new SCGColor(255, 0, 0, 255);
        public static SCGColor Cyan = new SCGColor(255, 0, 255, 255);
        public static SCGColor Yellow = new SCGColor(255, 255, 255, 0);
        public static SCGColor Orange = new SCGColor(255, 255, 128, 0);
        public static SCGColor Purple = new SCGColor(255, 128, 0, 128);

        private static Random _rnd = new Random();
        public static SCGColor RandomColor
        {
            get
            {
                byte r = (byte)_rnd.Next(255);
                byte g = (byte)_rnd.Next(255);
                byte b = (byte)_rnd.Next(255);
                return new SCGColor(r, g, b);
            }
        }
    }
}
